package com.example.mntz.controller;

import com.example.mntz.model.Cm;
import com.example.mntz.model.LayuiTable;
import com.example.mntz.model.Post;
import com.example.mntz.model.PostBq;
import com.example.mntz.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/post")
public class PostController {
    @Autowired
    PostService service;
    @GetMapping("/posts")
    public String posts(){
        return "post-posts";
    }
    @GetMapping("/myPosts")
    public String myPosts(){
        return "post-Myposts";
    }

    @GetMapping("/adminHtml")
    public String adminHtml(){
        return "admin-PostList";
    }

    @ResponseBody
    @GetMapping("/selectbq")
    public List<PostBq> selectbq(PostBq bq){
        List<PostBq> bqs = service.selectbq(bq);
        return bqs;
    }
    @ResponseBody
    @GetMapping("/selectposts")
    public LayuiTable<Post> selectposts(int limit,int page,String title,String bqs,Integer uid,Integer postStatu,String username){
        String[] bqss = null;
        if(bqs!=null&& !"".equals(bqs)){
            bqss = bqs.split(",");
        }
        LayuiTable<Post> post = service.selectposts(limit,page,title,bqss,uid,postStatu,username);
        return post;
    }

    @ResponseBody
    @GetMapping("/selectpostscount")
    public Integer selectpostscount(String title,String bqs,Integer uid,Integer postStatu){
        String[] bqss = null;
        if(bqs!=null&& !"".equals(bqs)){
            bqss = bqs.split(",");
        }
        Integer post = service.selectpostscount(title,bqss,uid,postStatu);
        return post;
    }
    //查看页面
    @GetMapping("/Post")
    public ModelAndView selectpost(int postid){
        ModelAndView model = new ModelAndView("post-post");
        Post post = service.selectPost(postid);
        model.addObject("post",post);
        return model;
    }
    //查找评论
    @ResponseBody
    @GetMapping("/postcm")
    public LayuiTable<Cm> postcm(int postid, int limit, int page){
        LayuiTable<Cm> cms =service.postcm(postid,limit,page);
        return cms;
    }
    @ResponseBody
    @PostMapping("/insterPostcm")
    public String insterPostcm(Cm cm){
        try {
            service.insterPostcm(cm);
            return "成功";
        }catch (Exception e){
            return "失败";
        }
    }
/*查看文章有多少赞*/
    @ResponseBody
    @PostMapping("/selectCountDz")
    public int selectCountDz(Integer postid){
        int count =0;
        Integer userid = null;
        try {
            count = service.selectcountcm(postid,userid);
        }catch (Exception e){
        }

        return count;
    }

    @ResponseBody
    @PostMapping("/insterPostDz")
    public String  insterPostDz(Integer postid,Integer userId){
        String ret = service.insterPostDz(postid,userId);
        return ret;
    }

    @ResponseBody
    @PostMapping("/deletePostDz")
    public String  deletePostDz(Integer postid){
        String ret = service.deletePostDz(postid);
        return ret;
    }
    @GetMapping("/bjPost")
    public ModelAndView bjPost(Integer postid){

        ModelAndView model = new ModelAndView("post-newPost");
        Post post = new Post();
        if(postid != null){
             post = service.selectPost(postid);
        }
        model.addObject("post",post);
        return model;
    }
    @ResponseBody
    @PostMapping("/insertpost")
    public void insertpost(Post post){
        Integer Postid = post.getPostid();
        if (Postid ==null){
            service.insterPost(post);
        }else {
            service.updatePost(post);
        }
    }
    @Transactional
    @DeleteMapping("/deleteposts")
    public void deleteposts(@RequestParam("postIds[]")  List<Integer> postIds){
        for (Integer a :postIds){
            deletePostDz(a);
        }
    }
}
